﻿using BLWCMS.Core.IBusiness;
using BLWCMS.Core.Models;
using Nancy.ModelBinding;

namespace BLWCMS.Web.Modules
{
    public class AdminModule : BaseModule
    {
        private readonly IAdminBusiness _adminBusiness;

        public AdminModule(IAdminBusiness adminBusiness)
            : base("/admin", true)
        {
            _adminBusiness = adminBusiness;

            Get["/"] = p => GetList();

            Get["/add"] = p => GetAdd();

            Post["/add"] = p => PostAdd();

            Get["/edit/{id:int}"] = p => GetEdit(p.id);

            Post["/edit"] = p => PostEdit();

            Post["/del/{id:int}"] = p => PostDel(p.id);
        }


        private dynamic GetList()
        {
            return View["Index", _adminBusiness.GetList()];
        }

        private dynamic GetAdd()
        {
            return View["Add"];
        }

        private dynamic PostAdd()
        {
            AdminModel adminModel = this.Bind<AdminModel>();
            return _adminBusiness.Add(adminModel);
        }

        private dynamic GetEdit(int id)
        {
            AdminModel adminModel = _adminBusiness.GetModel(id);
            return View["Edit", adminModel];
        }

        private dynamic PostEdit()
        {
            AdminModel adminModel = this.Bind<AdminModel>();
            return _adminBusiness.Edit(adminModel);
        }

        private dynamic PostDel(int id)
        {
            return _adminBusiness.Del(id);
        }
    }
}